package com.hds.modules.sys.service.base.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hds.common.utils.PageUtils;
import com.hds.common.utils.Query;
import com.hds.modules.app.service.UserService;
import com.hds.modules.sys.dao.base.SysMemberShipDao;
import com.hds.modules.sys.dao.base.SysMemberShipRecordsDao;
import com.hds.modules.sys.entity.base.SysMemberShipEntity;
import com.hds.modules.sys.entity.base.SysMemberShipRecordsEntity;
import com.hds.modules.sys.service.base.SysMemberShipRecordsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


/**
 * 会员
 *
 * @date 2018年06月18日 上午9:46:09
 */
@Service("sysMemberShipRecordsService")
public class SysMemberShipRecordsServiceImpl extends ServiceImpl<SysMemberShipRecordsDao, SysMemberShipRecordsEntity> implements SysMemberShipRecordsService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SysMemberShipRecordsDao sysMemberShipRecordsDao;

	@Autowired
	private SysMemberShipDao sysMemberShipDao;

	@Autowired
	private UserService userService;

	@Override
	public Integer save(SysMemberShipRecordsEntity memberShipRecords) {
		memberShipRecords.setCreateTime(new Date());
		memberShipRecords.setOrderNo(generateVipOrderNo());
		memberShipRecords.setStatus(SysMemberShipRecordsEntity.NOSUCCESS);
		logger.info("=====【会员升级新增订单】最终传入的入参报文，全字段=====:{}", JSONObject.toJSON(memberShipRecords));
		Integer r = sysMemberShipRecordsDao.insert(memberShipRecords);
		logger.info("=====插入结果r:{}=====", r);
		return r>0 ? memberShipRecords.getId() : null;
	}

	private String generateVipOrderNo() {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		return SysMemberShipRecordsEntity.suffix + sdf.format(new Date());
	}

	@Override
	public Boolean update(SysMemberShipRecordsEntity memberRecordsShip) {
		memberRecordsShip.setPayTime(new Date());
		memberRecordsShip.setStatus(SysMemberShipRecordsEntity.SUCCESS);
		Integer r = sysMemberShipRecordsDao.updateById(memberRecordsShip);
		return r>0 ? true : false;
	}


	@Override
	public void deleteBatch(Long[] ids) {
		// sysMemberShipDao.deleteBatchIds(ids);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String uid = (String)params.get("uid");

		Page<SysMemberShipRecordsEntity> page = this.selectPage(
				new Query<SysMemberShipRecordsEntity>(params).getPage(),
				new EntityWrapper<SysMemberShipRecordsEntity>()
						.eq("uid", uid)
		);

		for (SysMemberShipRecordsEntity sysMemberShipRecords: page.getRecords()) {
			Integer level = sysMemberShipRecords.getLevelId();
			SysMemberShipEntity memberShip= sysMemberShipDao.selectById(level);
			if(memberShip != null){
				sysMemberShipRecords.setSysMemberShip(memberShip);
			}
		}

		return new PageUtils(page);
	}


}
